<?php
class RequestsController extends AppController {

    var $name = 'Requests';

    function add() {
        if (!empty($this->data)) {
            function arrayFilter($value) {
                return !empty($value['sub_category_id']);
            }
            $this->data['RequestItem'] = array_filter($this->data['RequestItem'], 'arrayFilter');
            $this->Request->create();
            if ($this->Request->saveAll($this->data)) {
                $this->Session->setFlash(__('The request has been saved', true));
                $this->redirect(array('action' => 'add'));
            } else {
                $this->Session->setFlash(__('The request could not be saved. Please, try again.', true));
            }
        }
        $categories = $this->Request->RequestItem->SubCategory->Category->find('all');
        $subCategories = array('' => '');
        foreach ($categories as $category) {
            $subCategories["Category: {$category['Category']['name']}"] = array();
            foreach ($category['SubCategory'] as $subCategory) {
                $subCategories["Category: {$category['Category']['name']}"][$subCategory['id']] = $subCategory['name'];
            }
        }
        $this->set('subCategories', $subCategories);
        $this->set('members', $this->Request->Member->find('list', array('fields' => array('id', 'name_and_number'), 'order' => array('name'))));
    }

    function admin_index() {
        $this->Request->recursive = 0;
        $this->set('requests', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid request', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('request', $this->Request->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->Request->create();
            if ($this->Request->save($this->data)) {
                $this->Session->setFlash(__('The request has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The request could not be saved. Please, try again.', true));
            }
        }
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid request', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Request->save($this->data)) {
                $this->Session->setFlash(__('The request has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The request could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Request->read(null, $id);
        }
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for request', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->Request->delete($id)) {
            $this->Session->setFlash(__('Request deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('Request was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>